// pages/pressure-test-done/index.js
import { toPrevPage } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    ower_name: "",
    ower_phone: "",
    work_address: "",
    work_name: "",
    word_phone:"",
    branch_hearder:"",
    comp_name:"",
    product:"",
    product_name:"合格",
    work_quality:"合格",
    pressure_date:"",
    pressure_result:"上述检测依据,根据试压仪检测标准,所有的项目检测结果均合格",
    quality_code:"",
    pressure_name:"",
    pressure_phone:"",
    pressure_time:"",
    pressure_begin:"",
    pressure_end:"",
    pressure_drop:"",
    type:"1",
    showCalendar:false,
    ower_nameIsError: "",
    ower_phoneIsError: "",
    work_addressIsError: "",
    work_nameIsError: "",
    word_phoneIsError:"",
    branch_hearderIsError:"",
    comp_nameIsError:"",
    productIsError:"",
    product_nameIsError:"",
    work_qualityIsError:"",
    pressure_dateIsError:"",
    pressure_resultIsError:"",
    quality_codeIsError:"",
    pressure_nameIsError:"",
    pressure_phoneIsError:"",
    pressure_timeIsError:"",
    pressure_beginIsError:"",
    pressure_endIsError:"",
    pressure_dropIsError:"",
    productDict:[],
    productStatus:false,
  },

  handleower_nameIsError(event) {
    const value = event.detail;
    this.setData({
        ower_nameIsError: value ? "" : "请输入业主姓名",
    });
  },
  handleower_phoneIsError(event) {
    const value = event.detail;
    this.setData({
        ower_phoneIsError: value ? "" : "请输入业主电话",
    });
  },
  handlework_addressIsError(event) {
    const value = event.detail;
    this.setData({
        work_addressIsError: value ? "" : "请输入安装地址",
    });
  },
  handlework_nameIsError(event) {
    const value = event.detail;
    this.setData({
        work_nameIsError: value ? "" : "请输入水管安装工姓名",
    });
  },
  handleword_phoneIsError(event) {
    const value = event.detail;
    this.setData({
        word_phoneIsError: value ? "" : "请输入水管安装人电话",
    });
  },
  handlebranch_hearderIsError(event) {
    const value = event.detail;
    this.setData({
        branch_hearderIsError: value ? "" : "请输入销售网点负责人",
    });
  },
  handlecomp_nameIsError(event) {
    const value = event.detail;
    this.setData({
        comp_nameIsError: value ? "" : "请输入装饰公司名称",
    });
  },
  handleproductIsError(event) {
    const value = event.detail;
    this.setData({
        productIsError: value ? "" : "请输入检测产品",
    });
  },
  handleproduct_nameIsError(event) {
    const value = event.detail;
    this.setData({
        product_nameIsError: value ? "" : "请输入产品质量",
    });
  },
  handlework_qualityIsError(event) {
    const value = event.detail;
    this.setData({
        work_qualityIsError: value ? "" : "请输入安装焊接质量",
    });
  },
  handlepressure_dateIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_dateIsError: value ? "" : "请输入检测日期",
    });
  },
  handlepressure_resultIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_resultIsError: value ? "" : "请输入检测结果",
    });
  },
  handlequality_codeIsError(event) {
    const value = event.detail;
    this.setData({
        quality_codeIsError: value ? "" : "请输入质保卡号",
    });
  },
  handlepressure_nameIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_nameIsError: value ? "" : "请输入试压人员姓名",
    });
  },
  handlepressure_phoneIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_phoneIsError: value ? "" : "请输入试压人员电话",
    });
  },
  handlepressure_timeIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_timeIsError: value ? "" : "请输入保压时间",
    });
  },
  handlepressure_beginIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_beginIsError: value ? "" : "请输入保压起始压力值",
    });
  },
  handlepressure_endIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_endIsError: value ? "" : "请输入保压结束压力值",
    });
  },
  handlepressure_dropIsError(event) {
    const value = event.detail;
    this.setData({
        pressure_dropIsError: value ? "" : "请输入保压过程压降值",
    });
  },
  blurHandle(event) {
    const {
        name
    } = event.currentTarget.dataset
    const value = this.data.formData[name]
    this.setData({
        [`errorMessage.${name}`]: wxValidate.validateField(name, value)
    })
},
showPressure_dateSheet(){
    this.setData({ showCalendar: true });
  },
  onConfirm(event) {
    this.setData({
      showCalendar: false,
      pressure_date: this.formatDate(event.detail),
    })
  },
  onClose() {
    this.setData({ showCalendar: false });
  },
  formatDate(date) {
    date = new Date(date);
    console.log(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  hideProductStatusSheet() {
    this.setData({
        productStatus: false
    })
  },
  confirmProductStatusSheet(event) {
        const {
            value
        } = event.detail
        this.setData({
            product: value,
            productStatus: false
        })
    },
    showProductSheet(){
        this.setData({ productStatus: true });
    },
    product_nameChange(event) {
        this.setData({ product_name: event.detail });
    },
    work_qualityChange(event) {
        this.setData({ work_quality: event.detail });
    },
    typeChange(event) {
            this.setData({
                'type': event.detail,
            });
    },
  handleSubmit() {
    const {id,ower_name,ower_phone,work_address,work_name,word_phone,branch_hearder,comp_name,product,product_name,work_quality,pressure_date,pressure_result,quality_code,pressure_name,pressure_phone,type,pressure_time,pressure_begin,pressure_end,pressure_drop} = this.data;

    if (!ower_name) {
      this.setData({
        ower_nameIsError: "请输入业主姓名",
      });
      return;
    }
    if (!ower_phone) {
        this.setData({
            ower_phoneIsError: "请输入业主电话",
        });
        return;
    }
    if (!work_address) {
        this.setData({
            work_addressIsError: "请输入安装地址",
        });
        return;
    }
    if (!work_name) {
        this.setData({
            work_nameIsError: "请输入水管安装工姓名",
        });
        return;
    }
    if (!word_phone) {
        this.setData({
            word_phoneIsError: "请输入水管安装人电话",
        });
        return;
    }
    if (!branch_hearder) {
        this.setData({
            branch_hearderIsError: "请输入销售网点负责人",
        });
        return;
    }
    if (!comp_name) {
        this.setData({
            comp_nameIsError: "请输入装饰公司名称",
        });
        return;
    }
    if (!product) {
        this.setData({
            productIsError: "请输入检测产品",
        });
        return;
    }
   
    if (!product_name) {
        this.setData({
            product_nameIsError: "请输入产品质量",
        });
        return;
    }
    if (!work_quality) {
        this.setData({
            work_qualityIsError: "请输入安装焊接质量",
        });
        return;
    }
    if (!pressure_date) {
        this.setData({
            pressure_dateIsError: "请输入检测日期",
        });
        return;
    }
    if (!pressure_result) {
        this.setData({
            pressure_resultIsError: "请输入检测结果",
        });
        return;
    }
    if (!quality_code) {
        this.setData({
            quality_codeIsError: "请输入质保卡号",
        });
        return;
    }
    if (!pressure_name) {
        this.setData({
            pressure_nameIsError: "请输入试压人员姓名",
        });
        return;
    }
    if (!pressure_phone) {
        this.setData({
            pressure_phoneIsError: "请输入试压人员电话",
        });
        return;
    }
    if (!pressure_time) {
        this.setData({
            pressure_timeIsError: "请输入保压时间",
        });
        return;
    }
    if (!pressure_begin) {
        this.setData({
            pressure_beginIsError: "请输入保压起始压力值",
        });
        return;
    }
    if (!pressure_end) {
        this.setData({
            pressure_endIsError: "请输入保压结束压力值",
        });
        return;
    }
    if (!pressure_drop) {
        this.setData({
            pressure_dropIsError: "请输入保压过程压降值",
        });
        return;
    }
    const params = {
        id,
        ower_name,
        ower_phone,
        work_address,
        work_name,
        word_phone,
        branch_hearder,
        comp_name,
        product,
        product_name,
        work_quality,
        pressure_date,
        pressure_result,
        quality_code,
        pressure_name,
        pressure_phone,
        type,
        pressure_time,
        pressure_begin,
        pressure_end,
        pressure_drop,
    };

    wx.ajax.post("completePressureApply.action", params, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        const timer = setTimeout(() => {
          toPrevPage();

          clearTimeout(timer);
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },
  async  getAllFindVrTypeList(){
    let arrDic=['product']

    let list=await Promise.all(arrDic.map(async(type)=>{
       let {result}=await wx.ajax.get('findVrType.action', { type })
        return {
            type:`${type}Dict`,
            list:result.map(item => item.name)
        }
    }))
    
    list.map(item=>{
        this.setData({
            [item.type]:item.list
        })
    })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('1111');
    console.log(options.data);
    if (options.data) {
      const data = JSON.parse(options.data);
      this.setData({
        id:data.id,
        ower_name: data.ower_name,
        ower_phone: data.ower_phone,
        work_address: data.work_address,
        work_name: data.work_name,
        word_phone:data.word_phone,
        branch_hearder:data.branch_hearder,
        comp_name:data.comp_name,
        product:data.product,
        pressure_date:data.pressure_date,
        quality_code:data.quality_code,
        pressure_name:data.pressure_name,
        pressure_phone:data.pressure_phone,
        pressure_time:data.pressure_time,
        pressure_begin:data.pressure_begin,
        pressure_end:data.pressure_end,
        pressure_drop:data.pressure_drop,
      });
    }
    this.getAllFindVrTypeList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
